/* eslint-disable arrow-body-style */
/* eslint-disable max-len */
import { regex } from '@/shared/constants/regex';

export const validateEmail = (email: string) => {
  return String(email).toLowerCase().match(regex.email);
};

export const validateYoutubeLink = (link?: string): string | null => {
  if (!link) return null;
  const videoIdMatch = link.match(
    /(?:youtu\.be\/|youtube\.com\/(?:[^/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|\/(?:[cs]\/)?watch(?:er)?(?:\/.*?[&?])?v=)([^"&?/\s]{11})/,
  );
  if (!videoIdMatch) {
    return null;
  }
  const videoId = videoIdMatch[1];
  return `https://www.youtube.com/embed/${videoId}`;
};

export function formatDate(inputDate: string): string {
  if (inputDate.length < 10) {
    return '';
  }
  const parts = inputDate.split('.');

  if (parts.length !== 3) {
    return 'Invalid date';
  }

  const day = parts[0];
  const month = parts[1];
  const year = parts[2];

  const formattedDate = `${year}-${month}-${day}`;

  return formattedDate;
}

export function insertSpaces(str: string) {
  const regex = /(\+?\d)(\d{3})(\d{3})(\d{2})(\d{2})/;
  return str?.replace(regex, '$1 $2 $3 $4 $5');
}
